<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{height:1000px;}
        .box1{width:100px;height:100px;background: red;position: absolute;left:0;top:100px;}
        .box2{width:100px;height:100px;background: green;position: fixed;right:0;top:100px;}
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>
<script>

    const obox = document.querySelector(".box1");
    let oboxT = obox.offsetTop;

    // 滚走了的距离 + 原位置

    document.onscroll = function(){
        let t = document.documentElement.scrollTop;
        obox.style.top = (t + oboxT) + "px";
    }
    
</script>
</html>